package com.ulic.customerManApplication.util.excel;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import com.ulic.customerManApplication.util.config.ConfigInfo;

/**
 * UploadFile.java Description: Copyright (c) Department of Research and
 * Development/Beijing/北京中科汇联信息技术有限公司. All Rights Reserved.
 * 
 * @version 1.0 2012-5-4 下午04:38:10 李求智 (liqz@huilan.com) created
 */
public class UploadFile {

	/**
	 * Description:
	 * 
	 * @Version1.0 2012-5-4 下午04:38:08 李求智 (liqz@huilan.com) created
	 * @param attachment
	 * @param fileName
	 * @param date
	 * @return
	 * @throws Exception
	 */
	public static String upload(File attachment, String fileName, long date)
			throws Exception {
		String saveFileName = null;
		if (attachment != null && attachment.length() > 0) {
			// 文件路径
			// ServletActionContext.getRequest().getRealPath("/upload/");
			String srcPath = ConfigInfo.getPropertiesValue("uploadfile.path");
			// 生成新文件名
			saveFileName = date + fileName.substring(fileName.lastIndexOf("."));
			// 上传附件
			uploadFile(srcPath, saveFileName, attachment);
		} else {
			throw new Exception("文件:"
					+ fileName.substring(fileName.lastIndexOf("\\") + 1)
					+ "为空，请检查！");
		}
		return saveFileName;
	}

	public static String uploadImg(File attachment, String fileName, long date)
			throws Exception {
		String saveFileName = null;
		if (attachment != null && attachment.length() > 0) {
			// 文件路径
			@SuppressWarnings("deprecation")
			String srcPath = ServletActionContext.getRequest().getRealPath(
					"/image/candidate/");
			// 生成新文件名
			saveFileName = date + fileName;
			// 上传附件
			uploadFile(srcPath, saveFileName, attachment);
		} else {
			throw new Exception("上传文件失败！");
		}
		return saveFileName;
	}

	/**
	 * Description:
	 * 
	 * @Version1.0 2012-5-4 下午04:38:17 李求智 (liqz@huilan.com) created
	 * @param path
	 * @param fileName
	 * @param sourceFile
	 * @throws Exception
	 */
	public static void uploadFile(String path, String fileName, File sourceFile)
			throws Exception {
		File saveTo = new File(path + "/" + fileName);
		buildPath(path); // 建路径
		if (sourceFile.length() != 0) {
			copy(sourceFile, saveTo);
		}
	}

	/**
	 * Description:
	 * 
	 * @Version1.0 2012-5-4 下午04:38:19 李求智 (liqz@huilan.com) created
	 * @param ps_path
	 * @return
	 * @throws IOException
	 */
	public static String buildPath(String ps_path) throws IOException {
		File file = new File(ps_path);
		if (!file.exists())
			file.mkdirs();
		return file.getPath();
	}

	/**
	 * Description:
	 * 
	 * @Version1.0 2012-5-4 下午04:38:22 李求智 (liqz@huilan.com) created
	 * @param src
	 * @param dst
	 */
	public static void copy(File src, File dst) {
		int BUFFER_SIZE = 16 * 1024;
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				byte[] buffer = new byte[BUFFER_SIZE];
				out = new BufferedOutputStream(new FileOutputStream(dst),
						BUFFER_SIZE);
				if (src.exists()) {
					in = new BufferedInputStream(new FileInputStream(src),
							BUFFER_SIZE);
					int byteRead = 0;
					while ((byteRead = in.read(buffer)) > 0) {
						out.write(buffer, 0, byteRead);
					}
				}
			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


}
